package com.ssu.judea.travlog;

import com.ssu.judea.travlog.util.UrlImageConverter;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends TravlogActivity implements OnClickListener{
    private Button btnWrite;
	private Button btnView;
	private Button btnConfigure;
	private Button btnLogout;
	private LinearLayout profileLayout;
	private ImageView profileImage;
	private TextView profileName;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        btnWrite = (Button)findViewById(R.id.btnWrite);
        btnView = (Button)findViewById(R.id.btnView);
        btnConfigure = (Button)findViewById(R.id.btnConfigure);
        btnLogout = (Button)findViewById(R.id.btnLogout);
        profileLayout = (LinearLayout)findViewById(R.id.userProfileLayout);
        profileImage = (ImageView)findViewById(R.id.userProfileImage);
        profileName = (TextView)findViewById(R.id.userProfileId);
        
        
        btnWrite.setOnClickListener(this);
        btnView.setOnClickListener(this);
        btnConfigure.setOnClickListener(this);
        btnLogout.setOnClickListener(this);
    }
	
	@Override
	protected void onResume() {
		super.onResume();
		initializeAuthInfo();
	}
	
	private void initializeAuthInfo(){
		btnWrite.setEnabled(getTravlogApp().isAllowedUser);
		btnView.setEnabled(getTravlogApp().isAllowedUser);
		btnLogout.setEnabled(getTravlogApp().isAllowedUser);
		if(!getTravlogApp().isAllowedUser){
			getTravlogApp().alert("Authentication first using configure Menu");
			profileLayout.setVisibility(LinearLayout.GONE);
		}else{
			if(getTravlogApp().currentUser!=null){
				UrlImageConverter converter = new UrlImageConverter();
				profileImage.setImageDrawable(converter.urlToDrawable(getTravlogApp().currentUser.getProfileurl()));
				profileName.setText("@"+getTravlogApp().currentUser.getTwitterid());
				profileLayout.setVisibility(LinearLayout.VISIBLE);
			}
		}
	}
	
	@Override
	public void onClick(View v) {
		if(v==btnWrite){
			Intent i = new Intent(getApplicationContext(), WriteActivity.class);
			startActivityForResult(i, 1);
		}else if(v==btnView){
			Intent i = new Intent(getApplicationContext(), ViewActivity.class);
			startActivityForResult(i, 2);
		}else if(v==btnConfigure){
			Intent i = new Intent(getApplicationContext(), ConfigureActivity.class);
    		startActivityForResult(i, 3);
		}else if(v==btnLogout){
			getTravlogApp().doLogout(MainActivity.this);
		}
	}
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    	Log.v("changdoc","in Main Activity Result:"+requestCode+"resultCode:"+resultCode);
	    	if(requestCode==1){
	    		//after Write done
	    	}else if(requestCode==2){
	    		//after View done
	    	}else if(requestCode==3){
	    		//after Config done
	    		if(getTravlogApp().isAllowedUser){
	    			getTravlogApp().alert("@"+getTravlogApp().currentUser.getTwitterid() + " Authentication Success");
	    		}else{
	    			getTravlogApp().alert("Authentication Fail");
	    		}
	    	}
    }

	public void completeLogout(String result) {
		initializeAuthInfo();
	}

	
}